﻿using System;
using System.Net;
using ImageTools;
using ImageTools.IO;
using ImageTools.IO.Png;
using System.IO;
using Calligraphy.Models;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using CalligraphyWeb;
using System.Drawing.Text;
using CalligraphyWeb.Classes;

namespace ThuPhap.Class
{
    public class ExportImage
    {
        public static string Export(item project)
        {
            
            string exDir = HttpRuntime.AppDomainAppPath + "Data\\ItemImages\\";
            string bgDir = HttpRuntime.AppDomainAppPath + "Data\\Backgrounds\\";
            string backgroundPath = "";
            if (project.background.Contains("/"))
            {
                backgroundPath = HttpRuntime.AppDomainAppPath + project.background.Replace("/", "\\");
            }
            else {
                backgroundPath = bgDir + project.background;
            }

            
            string filename = Guid.NewGuid() + ".png";
            Graphics result;
            Image background;
            if (File.Exists(backgroundPath))
            {
                background = Image.FromFile(backgroundPath);
                result = Graphics.FromImage(background);
            }
            else { 
                background = new Bitmap(480, 800);
                result = Graphics.FromImage(background);
            }

            foreach (var iter in project.itemControls)
            {

                var x = iter.C_left.Value;
                var y = iter.C_top.Value;
                Color color = ColorTranslator.FromHtml(('#' + iter.foreground));
                Brush foreground =
                    new SolidBrush(color);
                var font = Calligraphy.Models.FontFamily.Search(name: iter.fontFamily);
                var fontPath = HttpRuntime.AppDomainAppPath +
                   "Data\\Fonts\\" + font[0].filename;
                
                //install fonts
                PrivateFontCollection fontCollection;
                System.Drawing.FontFamily fontFamily = FontHelper.LoadFontFamily(fontPath, out fontCollection);

                var fontStyle = iter.isItalic.Value ? FontStyle.Italic
 : FontStyle.Regular;
                var fontWeight = iter.isBold.Value ? FontStyle.Bold : FontStyle.Regular;
                var align = iter.align == 0 ?  ContentAlignment.TopCenter
                    : (iter.align == 3) ? ContentAlignment.TopRight :
                    ContentAlignment.TopLeft;

                var fontSize = iter.fontSize;
                var fontObject = new Font(fontFamily, (float)(fontSize.Value/3),
                    fontStyle | fontWeight);
                RectangleF rectF = new RectangleF((float)iter.C_left.Value
                    , (float)iter.C_top.Value, (float)iter.width.Value, (float)iter.height.Value);

                result.DrawString(iter.text, fontObject, foreground, rectF);

                //clear memory
                fontCollection.Dispose();
                fontFamily.Dispose();
                fontObject.Dispose();
            }

            background.Save(exDir + filename, ImageFormat.Png);


            return filename;
        }

        public static string Export(template project)
        {

            string exDir = HttpRuntime.AppDomainAppPath + "Data\\ItemImages\\";
            string bgDir = HttpRuntime.AppDomainAppPath + "Data\\Backgrounds\\";
            string templateBackgroundDir = HttpRuntime.AppDomainAppPath + "Data\\TemplateBackgrounds\\";
            string TemplateImageDir = HttpRuntime.AppDomainAppPath + "Data\\TemplateImages\\";

            string backgroundPath = "";
            if (project.image.Contains("/"))
            {
                backgroundPath = HttpRuntime.AppDomainAppPath + project.image.Replace("/", "\\");
            }
            else
            {
                backgroundPath = templateBackgroundDir + project.image;
            }


            string filename = Guid.NewGuid() + ".png";
            Graphics result;
            Image background;
            if (File.Exists(backgroundPath))
            {
                background = Image.FromFile(backgroundPath);
                result = Graphics.FromImage(background);
            }
            else
            {
                background = new Bitmap(480, 800);
                result = Graphics.FromImage(background);
            }

            foreach (var iter in project.templateControls)
            {

                var x = iter.C_left.Value;
                var y = iter.C_top.Value;
                Color color = ColorTranslator.FromHtml(('#' + iter.foreground));
                Brush foreground =
                    new SolidBrush(color);
                var font = Calligraphy.Models.FontFamily.Search(name: iter.fontFamily);
                var fontPath = HttpRuntime.AppDomainAppPath +
                   "Data\\Fonts\\" + font[0].filename;

                //install fonts
                PrivateFontCollection fontCollection;
                System.Drawing.FontFamily fontFamily = FontHelper.LoadFontFamily(fontPath, out fontCollection);

                var fontStyle = iter.isItalic.Value ? FontStyle.Italic
 : FontStyle.Regular;
                var fontWeight = iter.isBold.Value ? FontStyle.Bold : FontStyle.Regular;
                var align = iter.align == 0 ? ContentAlignment.TopCenter
                    : (iter.align == 3) ? ContentAlignment.TopRight :
                    ContentAlignment.TopLeft;

                var fontSize = iter.fontSize;
                var fontObject = new Font(fontFamily, (float)(fontSize.Value / 3),
                    fontStyle | fontWeight);
                RectangleF rectF = new RectangleF((float)iter.C_left.Value
                    , (float)iter.C_top.Value, (float)iter.width.Value, (float)iter.height.Value);
                result.DrawString(iter.text, fontObject, foreground, rectF);

                //clear memory
                fontCollection.Dispose();
                fontFamily.Dispose();
                fontObject.Dispose();
            }

            background.Save(TemplateImageDir + filename, ImageFormat.Png);


            return filename;
        }
    }
}
